package ninegag.storm.me.testloginmvp.view;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import ninegag.storm.me.testloginmvp.R;
import ninegag.storm.me.testloginmvp.presenter.IUserPresenter;
import ninegag.storm.me.testloginmvp.presenter.UserPresenter;

/**
 * view层的实体类就是获取控件,设计监听,创建presenter层的实例,调用presenter层的方法
 */
public class UserLoginActivity extends AppCompatActivity implements IUserView {
    private android.widget.EditText mEtid;
    private android.widget.EditText mEtname;
    private android.widget.EditText mEtpassword;
    private android.widget.Button mSave;
    private android.widget.Button mLoad;
    private IUserPresenter mUserPresenter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mUserPresenter = new UserPresenter(this);
        initView();
        initListener();
    }

    private void initListener() {
        mSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mUserPresenter.saveInfo();
            }
        });

        mLoad.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mUserPresenter.loadInfo();
            }
        });
    }

    private void initView() {
        this.mLoad = (Button) findViewById(R.id.load);
        this.mSave = (Button) findViewById(R.id.save);
        this.mEtpassword = (EditText) findViewById(R.id.et_password);
        this.mEtname = (EditText) findViewById(R.id.et_name);
        this.mEtid = (EditText) findViewById(R.id.et_id);
    }

    @Override
    public int getUserId() {
        return Integer.parseInt(mEtid.getText().toString());
    }

    @Override
    public String getUserName() {
        return mEtname.getText().toString();
    }

    @Override
    public String getUserPassword() {
        return mEtpassword.getText().toString();
    }

    @Override
    public void setUserName(String name) {
        mEtname.setText(name);
    }

    @Override
    public void setUserPassword(String password) {
        mEtpassword.setText(password);
    }
}
